﻿using System;
using System.Web.UI.WebControls;
using Olympic_email_to_future.Module;
using Olympic_email_to_future.BLLFactory;
using Olympic_email_to_future.IBLL;

namespace FutureMeWeb.FutureMe_Admin
{
    public partial class userInfo : System.Web.UI.Page
    {
        public UserInfo userinfo = new UserInfo();
        public IUserBLL user = BusinessFactory.CreateUserBLL();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
            
        }

        //删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[0].Text);

            user.delete(id,userinfo);

            GridView1.EditIndex = -1;
            bind();
        }

        //更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[0].Text);
            if (GridView1.Rows[e.RowIndex].Cells[3].Text == "普通用户")
            {
                userinfo.userRole = 1;
            }
            else if (GridView1.Rows[e.RowIndex].Cells[3].Text == "管理员")
            {
                userinfo.userRole = 0;
            }
            userinfo.userState = 1;

            user.update(id,userinfo);

            GridView1.EditIndex = -1;
            bind();

        }

        //取消
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();
        }

        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            bind();
        }
        

        //绑定
        public void bind()
        {
            GridView1.DataSource = user.getUsers();
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string userType="";
            //如果是绑定数据行 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标经过时，行背景色变 
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
                //鼠标移出时，行背景色变 
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");


                if (e.Row.Cells[3].Text == "0" || e.Row.Cells[3].Text == "1")
                {
                    int x = Convert.ToInt32(e.Row.Cells[3].Text);//第4列
                    if (x == 0)
                    {
                        userType = "普通用户";

                    }
                    else
                    {
                        userType = "管理员";
                    }
                    e.Row.Cells[3].Text = userType;
                }
                if (e.Row.Cells[2].Text == "0" || e.Row.Cells[2].Text == "1")
                {
                    int x = Convert.ToInt32(e.Row.Cells[2].Text);//第3列
                    if (x == 0)
                    {
                        userType = "未激活";

                    }
                    else
                    {
                        userType = "已激活";
                    }
                    e.Row.Cells[2].Text = userType;
                }
               

                //当有编辑列时，避免出错，要加的RowState判断 
                /*if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((ImageButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除\"" + e.Row.Cells[2].Text + "\"吗?')");
                }
                */
            }

        }

    }
}